package com.itrencai.d6_set.doudizhu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DouDiZhu {
    //1.准备一个集合容器装54张牌对象
    public static List<Card> allCards=new ArrayList<>();
    //2.静态代码块初始化牌数据
    //静态代码块有static修饰，属于类，与类一起优先加载一次，，自动触发执行
    //作用：初始化静态资源
    static{
        //3.定义点数，个数确定，类型确定，使用数组
        String[] sizes={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //4.定义花色，个数确定，类型确定，使用数组
        String[] colors={"♥","♣","♦","♠"};
        //5.组合花色和点数
        int index=0;//记录牌的大小
        for (String size : sizes) {
            index ++;
            for (String color : colors) {
                //6.封装牌对象
                Card c=new Card(size,color,index);
                //7.存入到集合中
                allCards.add(c);
            }
        }
        //8.大小王存入到集合当中去
        Card c1=new Card("","大王",++index);
        Card c2=new Card("","小王",++index);
        Collections.addAll(allCards,c1,c2);
        System.out.println("新牌："+allCards);
    }

    public static void main(String[] args) {
        //9.洗牌
        Collections.shuffle(allCards);//可以随机打乱
        System.out.println("洗牌后："+allCards);
        //10.发牌 定义三个玩家，每个玩家的牌也是一个集合容器
        List<Card> linghuchong=new ArrayList<>();//创建三个玩家对象
        List<Card> jiumozhi=new ArrayList<>();
        List<Card> renyingying=new ArrayList<>();
        //11.开始发牌(从集合中发出51张牌给三个玩家，剩余三张作为底牌)
        for (int i = 0; i < allCards.size()-1; i++) {
            //先拿到当前牌对象
            Card c=allCards.get(i);
            if(i%3==0){//第一个人
                linghuchong.add(c);
            }else if(i%3==1){//第二个人
                jiumozhi.add(c);
            }else{
                renyingying.add(c);
            }
        }
        //12.拿到最后三张底牌(把最后三张牌截取成一个子集合)
        List<Card> lastThreeCards=allCards.subList(allCards.size()-3,allCards.size());
        //13.给玩家的牌排序(从大到小)
        sortCards(linghuchong);
        sortCards(jiumozhi);//sort可以排序
        sortCards(renyingying);
        //14.输出玩家的牌
        System.out.println("令狐冲"+linghuchong);//输出玩家和各自的牌
        System.out.println("鸠摩智"+jiumozhi);
        System.out.println("任盈盈"+renyingying);
        System.out.println("最后三张底牌"+lastThreeCards);



    }
    //给牌排序方法
/*    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                //知道牌的大小，才能制定规则
                return o2.getIndex()-o1.getIndex();//降序
            }
        });
    }*/

    //排序方法的制定，lambda表达式 collections工具类中的sort方法实现
    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, (o1, o2) -> o2.getIndex() - o1.getIndex());
    };




}
